package common;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragment extends Fragment {

    protected View mRootView = null;

    // 页面名称，默认页面
    protected String mPageTitleName = BaseFragment.class.getSimpleName();

    public BaseFragment() {
        setPageTitleName(this.getClass().getSimpleName());
    }

    public BaseFragment(String pageTitle) {
        setPageTitleName(pageTitle);
    }

    public BaseFragment(Context context, int pageTitleId) {
        setPageTitleName(context, pageTitleId);
    }

    /**
     * 获取Fragment根资源布局文件ID
     *
     * @return
     */
    public abstract int obtainLayoutResource();

    /**
     * 获取Fragment根视图
     *
     * @return
     */
    public View getRootView() {
        return mRootView;
    }

    /**
     * 获取页面名称
     *
     * @return
     */
    public String getPageTitleName() {
        return mPageTitleName;
    }

    /**
     * 设置页面标题名称
     *
     * @param pageTitleName
     */
    public void setPageTitleName(String pageTitleName) {
        this.mPageTitleName = pageTitleName;
    }

    /**
     * 设置页面标题名称
     *
     * @param pageTitleName
     */
    public void setPageTitleName(Context context, int pageTitleName) {
        CharSequence text = context.getResources().getText(pageTitleName);
        this.mPageTitleName = text.toString();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(obtainLayoutResource(), container, false);
        findViews(mRootView);
        initData();
        return mRootView;
    }

    /**
     * 不使用注解时使用该方法获取视图对象
     *
     * @param rootView
     */
    public abstract void findViews(View rootView);

    public abstract void loadData(Object data);

    /**
     * 初始化数据
     */
    public abstract void initData();
}
